import com.mongodb.Block;
import com.mongodb.MongoClient;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.model.Filters;
import org.bson.Document;
import org.bson.conversions.Bson;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

import java.util.LinkedList;

public class MongoDB {
    static int index = 1;


    /**
     * get students from mongodb store them in Linklist
     *
     * @throws ParseException idk
     */
    public static LinkedList<Student> GetStudentFromMongo() throws ParseException {
        LinkedList<Student> students = new LinkedList<>();
        LinkedList<String> str = new LinkedList<>();

        // link mongo db
        MongoClient mongoClient = new MongoClient("localhost", 27017);
        MongoCollection<Document> collection = mongoClient.getDatabase("myTest").getCollection("students");

        // get json strings store in LinkedList
        FindIterable iter = collection.find();
        iter.forEach(new Block<Document>() {
            @Override
            public void apply(Document o) {
//                System.out.println(o.toJson());
                str.addLast(o.toJson());
            }
        });

        //get Student from json strings using parser
        for (var item : str) {
            JSONParser parser = new JSONParser();
            JSONObject json = (JSONObject) parser.parse(item);
            String no = (String) json.get("_id");
            String name = (String) json.get("name");
            String sex = (String) json.get("gender");
            String classs = (String) json.get("class");

            students.addLast(new Student(no, name, sex, classs));
        }

        // out put students
        for (var item : students)
            System.out.println(item.toString());

        return students;
    }


    public static void main(String[] args) throws ParseException {
//        String no = "007";
//        GetStudentFromMongo();
//        System.out.println(get序号_总数(no));
//        InsertStudent(a);
//        Delstudent("009");
        Student a = new Student("008", "kasusa", "MFFF", "0102");
        Updatestudent(a);
    }

    public static void Updatestudent(Student a) {
        // link mongo db
        MongoClient mongoClient = new MongoClient("localhost", 27017);
        MongoCollection<Document> collection = mongoClient.getDatabase("myTest").getCollection("students");

        // filter and del
        Bson filter = Filters.eq("_id", a.no);
        Document document = new Document("$set",
                new Document()
                        .append("_id", a.no)
                        .append("name", a.name)
                        .append("gender", a.sex)
                        .append("class", a.classs)
        );

        //修改单个文档
        collection.updateOne(filter, document);
    }

    public static void Delstudent(String strNo) {
        // link mongo db
        MongoClient mongoClient = new MongoClient("localhost", 27017);
        MongoCollection<Document> collection = mongoClient.getDatabase("myTest").getCollection("students");

        // filter and del
        Bson filter = Filters.eq("_id", strNo);
        collection.deleteOne(filter);
    }

    public static void InsertStudent(Student a) {
        // link mongo db
        MongoClient mongoClient = new MongoClient("localhost", 27017);
        MongoCollection<Document> collection = mongoClient.getDatabase("myTest").getCollection("students");

        Document document =
                new Document()
                        .append("_id", a.no)
                        .append("name", a.name)
                        .append("gender", a.sex)
                        .append("class", a.classs);
        collection.insertOne(document);
    }
}
